package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.LogUtils
import com.bytedance.applog.AppLog
import com.bytedance.applog.InitConfig
import com.bytedance.applog.util.UriConstants
import com.kwai.monitor.log.TurboAgent
import com.kwai.monitor.log.TurboConfig
import com.lykj.lib_base.adapter.viewpager.FragmentSPAdapter
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.bus.FlowBus
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.utils.ChannelUtils
import com.lykj.llw.box.R
import com.lykj.llw.box.databinding.ActHomeBinding
import com.lykj.llw.box.ui.fragment.HomeFragment
import com.lykj.llw.box.ui.fragment.MineFragment
import com.lykj.llw.box.ui.fragment.TradingFragment
import com.lykj.llw.box.ui.fragment.VipFragment

/**
 * ================================================
 * 首页
 * ================================================
 *
 * @author gosha
 * @date 2023/10/12-17:38
 */
class HomeActivity : BaseActivity() {
    lateinit var binding: ActHomeBinding

    val mFragments = ArrayList<Fragment>()
    var jumpPage = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setOnInterceptBack(interceptBack = true)
        binding = ActHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(Bundle())
    }

    override fun initUI() {
        //根据参数动态显示隐藏账号交易
        FlowBus.with<Boolean>(BConstants.BUS_KEY.HIDE_TRADING).register(this) {
            if (it) {
                binding.vNavTrading.visibility = View.VISIBLE
                binding.tvNavTrading.visibility = View.VISIBLE
                binding.ivNavTrading.visibility = View.VISIBLE
            } else {
                binding.vNavTrading.visibility = View.GONE
                binding.tvNavTrading.visibility = View.GONE
                binding.ivNavTrading.visibility = View.GONE
            }
        }

        mFragments.add(HomeFragment())
        mFragments.add(VipFragment())
        mFragments.add(TradingFragment())
        mFragments.add(MineFragment())

        //设置viewpager
        val adapter = FragmentSPAdapter(supportFragmentManager, mFragments)
        binding.cvpContent.offscreenPageLimit = 3
//
//        ngGuidePageTransformer.setCurrentItem(this,0,mFragments)
//        binding.cvpContent.setPageTransformer(true,ngGuidePageTransformer)
        binding.cvpContent.adapter = adapter
//        switchNav(0)
    }

    override fun initData() {
        if (null != intent) {
            val authLogin = intent.getBooleanExtra("authLogin", false)
            val backScheme = intent.getStringExtra("back")
            if (authLogin) {
                LogUtils.e("需要授权认证.")
                val intent = Intent()
                intent.data = Uri.parse("$backScheme://cqkj.com")
                startActivity(intent)
            }
        }

        ChannelUtils.initReadConfig(this)

        FlowBus.with<String>(BConstants.BUS_KEY.LOGIN_SUCCESS).register(this) {
            switchNav(jumpPage)
        }

        FlowBus.with<String>(BConstants.BUS_KEY.LOGOUT_SUCCESS).register(this) {
            switchNav(0)
        }

        FlowBus.with<Int>(BConstants.BUS_KEY.HOME_SWITCH_NAV).register(this) {
            switchNav(it)
        }

        FlowBus.with<Int>(BConstants.BUS_KEY.HOME_SWITCH_NAV_OTHER).register(this) {
            jumpPage = it
        }

        initStat()
    }

    private fun initStat() {
        val config = InitConfig("576653", "default")
        /* 国内: DEFAULT */
        config.setUriConfig(UriConstants.DEFAULT)
        config.setLogger { p0, p1 ->
            LogUtils.e("巨量:$p0")
        }
        config.isAndroidIdEnabled = false
        config.isMacEnable = false
        config.setImeiEnable(false)
        config.isAutoTrackEnabled = false
        config.setLogEnable(false)
        config.setEnablePlay(true) // 是否开启游戏模式，游戏APP建议设置为 true
        config.isAbEnable = false // 是否开启A/B Test功能

        config.setAutoStart(true)
        AppLog.setEncryptAndCompress(false)
        AppLog.init(this, config)

        TurboAgent.init(
            TurboConfig.TurboConfigBuilder.create(this)
                .setAppId("88998")
                .setAppName("zanqu")
                .setAppChannel("default")
                .setEnableDebug(true)
                .build()
        )
    }

    override fun setListener() {
        binding.vNavGame.setOnClickListener {
            jumpPage = 0
            switchNav(0)
        }
        binding.vNavVip.setOnClickListener {
            if (BaseApplication.instance?.getUserInfo()?.token.isNullOrBlank()) {
                jumpPage = 1

                startActivity(Intent(this, MobileLoginActivity::class.java))
            } else {
                switchNav(1)
            }
        }
        binding.vNavTrading.setOnClickListener {
            jumpPage = 2
            switchNav(2)
        }
        binding.vNavMine.setOnClickListener {
            if (BaseApplication.instance?.getUserInfo()?.token.isNullOrBlank()) {
                jumpPage = 3

                startActivity(Intent(this, MobileLoginActivity::class.java))
            } else {
                switchNav(3)
            }
        }
    }

    //重置底部导航栏状态
    private fun restNav() {
        binding.ivNavGame.setImageResource(R.mipmap.ic_nav_home_unselect)
        binding.ivNavVip.setImageResource(R.mipmap.ic_nav_vip_unselect)
        binding.ivNavTrading.setImageResource(R.mipmap.ic_nav_trading_unselect)
        binding.ivNavMine.setImageResource(R.mipmap.ic_nav_mine_unselect)


        binding.tvNavGame.setTextColor(ContextCompat.getColor(this, R.color.black_FF212121))
        binding.tvNavVip.setTextColor(ContextCompat.getColor(this, R.color.black_FF212121))
        binding.tvNavTrading.setTextColor(ContextCompat.getColor(this, R.color.black_FF212121))
        binding.tvNavMine.setTextColor(ContextCompat.getColor(this, R.color.black_FF212121))
    }

    //切换到导航栏到指定页面
    private fun switchNav(position: Int) {
        restNav()
        when (position) {
            0 -> {
                binding.ivNavGame.setImageResource(R.mipmap.ic_nav_home_selected)
                binding.tvNavGame.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }

            1 -> {
                binding.ivNavVip.setImageResource(R.mipmap.ic_nav_vip_selected)
                binding.tvNavVip.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }

            2 -> {
                binding.ivNavTrading.setImageResource(R.mipmap.ic_nav_trading_selected)
                binding.tvNavTrading.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }

            3 -> {
                binding.ivNavMine.setImageResource(R.mipmap.ic_nav_mine_selected)
                binding.tvNavMine.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
        }
        binding.cvpContent.setCurrentItem(position, true)
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}